package team.seekers.skill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.skill.entity.dao.UserSeekSkill;
import team.seekers.skill.entity.vo.fore.UserLearnStatusVO;
import team.seekers.skill.mapper.UserSeekSkillMapper;
import team.seekers.skill.service.SkillService;
import team.seekers.skill.service.UserSeekSkillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  展示用户在求存者模块的情况
 * </p>
 *
 * @author hbc
 * @since 2021-02-04
 */
@Service
public class UserSeekSkillServiceImpl extends ServiceImpl<UserSeekSkillMapper, UserSeekSkill> implements UserSeekSkillService {

    @Autowired
    private SkillService skillService;

    // 列出该用户在每个类型的学习情况，按照已学技能数占全部技能数的百分比的形式展现
    @Override
    public UserLearnStatusVO getLearnStatusByUserId(String userId){
        // 统计总技能数
        Integer skillAmount = skillService.count(null);
        // 计算各个类型的占比
        QueryWrapper<UserSeekSkill> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserSeekSkill userSeekSkill = getOne(wrapper);
        UserLearnStatusVO userLearnStatusVO = new UserLearnStatusVO();
        if(skillAmount.equals(0)){
            userLearnStatusVO.setComputerLearnPercentage(0);
            userLearnStatusVO.setCodeLearnPercentage(0);
            userLearnStatusVO.setLifeLearnPercentage(0);
            userLearnStatusVO.setBeautyLearnPercentage(0);
            userLearnStatusVO.setMoneyLearnPercentage(0);
            return userLearnStatusVO;
        }
        Integer computerPercentage = (int)(userSeekSkill.getStartComputerSkill()*0.3+ userSeekSkill.getEndComputerSkill())/skillAmount;
        Integer codePercentage = (int)(userSeekSkill.getStartCodeSkill()*0.3+ userSeekSkill.getEndCodeSkill())/skillAmount;
        Integer lifePercentage = (int)(userSeekSkill.getStartLifeSkill()*0.3+ userSeekSkill.getEndLifeSkill())/skillAmount;
        Integer beautyPercentage = (int)(userSeekSkill.getStartBeautySkill()*0.3+ userSeekSkill.getEndBeautySkill())/skillAmount;
        Integer moneyPercentage = (int)(userSeekSkill.getStartMoneySkill()*0.3+ userSeekSkill.getEndMoneySkill())/skillAmount;
        // 封装数据
        userLearnStatusVO.setComputerLearnPercentage(computerPercentage);
        userLearnStatusVO.setCodeLearnPercentage(codePercentage);
        userLearnStatusVO.setLifeLearnPercentage(lifePercentage);
        userLearnStatusVO.setBeautyLearnPercentage(beautyPercentage);
        userLearnStatusVO.setMoneyLearnPercentage(moneyPercentage);
        return userLearnStatusVO;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean addStartByType(String userId,String type) {
        QueryWrapper<UserSeekSkill> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserSeekSkill userSeekSkill = getOne(wrapper);
        switch (type){
            case "computer":
                userSeekSkill.setStartComputerSkill(userSeekSkill.getStartComputerSkill()+1);
                break;
            case "code":
                userSeekSkill.setStartCodeSkill(userSeekSkill.getStartCodeSkill()+1);
                break;
            case "life":
                userSeekSkill.setStartLifeSkill(userSeekSkill.getStartLifeSkill()+1);
                break;
            case "beauty":
                userSeekSkill.setStartBeautySkill(userSeekSkill.getStartBeautySkill()+1);
                break;
            case "money":
                userSeekSkill.setStartMoneySkill(userSeekSkill.getStartMoneySkill()+1);
                break;
            default:
                return false;
        }
        userSeekSkill.setStartSkill(userSeekSkill.getStartSkill()+1);
        boolean update = updateById(userSeekSkill);
        return update;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean addEndByType(String userId, String type) {
        QueryWrapper<UserSeekSkill> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserSeekSkill userSeekSkill = getOne(wrapper);
        switch (type){
            case "computer":
                userSeekSkill.setStartComputerSkill(userSeekSkill.getStartComputerSkill()-1);
                userSeekSkill.setEndComputerSkill(userSeekSkill.getEndComputerSkill()+1);
                break;
            case "code":
                userSeekSkill.setStartCodeSkill(userSeekSkill.getStartCodeSkill()-1);
                userSeekSkill.setEndCodeSkill(userSeekSkill.getEndCodeSkill()+1);
                break;
            case "life":
                userSeekSkill.setStartLifeSkill(userSeekSkill.getStartLifeSkill()-1);
                userSeekSkill.setEndLifeSkill(userSeekSkill.getEndLifeSkill()+1);
                break;
            case "beauty":
                userSeekSkill.setStartBeautySkill(userSeekSkill.getStartBeautySkill()-1);
                userSeekSkill.setEndBeautySkill(userSeekSkill.getEndBeautySkill()+1);
                break;
            case "money":
                userSeekSkill.setStartMoneySkill(userSeekSkill.getStartMoneySkill()-1);
                userSeekSkill.setEndMoneySkill(userSeekSkill.getEndMoneySkill()+1);
                break;
            default:
                return false;
        }
        userSeekSkill.setStartSkill(userSeekSkill.getStartSkill()-1);
        userSeekSkill.setEndSkill(userSeekSkill.getEndSkill()+1);
        boolean update = updateById(userSeekSkill);
        return update;
    }
}
